import { getMenuListByRoleId, getAllMenuByRoleId } from '@/api/url'
import Mock from 'mockjs'
import { menuItemList } from '../base'

Mock.mock(RegExp(getMenuListByRoleId), 'post', function (options) {
  const roleId = JSON.parse(options.body).roleId || ''
  if (!roleId) {
    return Mock.mock({ code: 500, data: '', msg: '获取菜单列表失败' })
  }
  if (parseInt(roleId) === 1) {
    // 超级管理员
    return Mock.mock({ code: 200, data: menuItemList, msg: '获取菜单列表成功' })
  } else if (parseInt(roleId) === 2) {
    // 编辑
    return Mock.mock({
      code: 200,
      data: menuItemList,
      msg: '获取菜单列表成功',
    })
  } else {
    return Mock.mock({
      code: 500,
      data: '',
      msg: '用户角色编号错误,无法获取有效的菜单列表',
    })
  }
})

Mock.mock(RegExp(getAllMenuByRoleId), 'post', function (options) {
  const roleId = JSON.parse(options.body).roleId || ''
  if (!roleId) {
    return Mock.mock({ code: 500, data: '', msg: '获取菜单列表失败' })
  }
  const allRoutes = [...menuItemList]
  allRoutes.forEach((it) => {
    it.isSelect = parseInt(roleId) === 1 || it.menuUrl.indexOf('authority') === -1
    it.children.forEach((child) => {
      child.isSelect = parseInt(roleId) === 1 || child.menuUrl.indexOf('authority') === -1
    })
  })
  return Mock.mock({ code: 200, data: allRoutes, msg: '获取菜单列表成功' })
})

Mock.mock(RegExp(getAllMenuByRoleId), 'post', function (options) {
  const roleEntity = JSON.parse(options.body)
  return Mock.mock({ code: 200, data: '', msg: '保存角色成功成功' })
})
